package com.zmn.plat.business.impl.sendtemplate;

import java.util.Objects;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.utils.MQPushUtil;
import com.zmn.plat.business.interfaces.sendtemplate.SendSmsInfoBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.manager.constant.ZmnSmsConsts;
import com.zmn.plat.model.entity.message.MsgTemplate;
import com.zmn.plat.model.entity.push.ZmnSms;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述：发送百度模板消息 模板消息
 *
 * @author wangjie
 * @since 2020/6/19 10:42
 **/
@Slf4j
public class BaiduSendSmsInfoBServiceImpl implements SendSmsInfoBService {

    private static final String TAG = "发送百度模板消息";

    private volatile static BaiduSendSmsInfoBServiceImpl aliSendSmsInfoBService;

    private BaiduSendSmsInfoBServiceImpl() {}

    @Override
    public Integer doSend(MsgTemplate msgTemplate, ZmnSms zmnSms) {

        // 验证必要条件
        if (StringUtil.isBlank(zmnSms.getSceneId())) {
            log.error("[{}],发送失败，场景类型Id不能为null。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_BD_NO_SCENEID, ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_BD_NO_SCENEID);
        }
        if (StringUtil.isBlank(zmnSms.getToUserId()) && StringUtil.isNotBlank(zmnSms.getOpenId())) {
            log.error("[{}],发送失败，接受者不能为null。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_BD_NO_USERID_OR_OPENID, ZmnSmsConsts.MSG_NO_TEMPLATE_MESSAGE_ERROR_STR);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_BD_NO_USERID_OR_OPENID);
        }
        ResponseDTO<Boolean> response = MQPushUtil.sendBaiduAppTemplateInfo(zmnSms.getBaiduToken(), msgTemplate.getBaiduTempId(), zmnSms.getToUserId(), zmnSms.getToUserOpenId(),
            msgTemplate.getBaiduMessage(), zmnSms.getSceneId(), msgTemplate.getSceneType(), zmnSms.getBaiduPage(), null);

        if (response.isSuccess()) {
            log.info("[{}],发送成功。 code:{},msg:{}", TAG, ZmnSmsConsts.MSG_SEND_SUCCESS, ZmnSmsConsts.MSG_SEND_SUCCESS_STR);
            return ZmnSmsConsts.MSG_SEND_SUCCESS;
        } else {
            log.info("[{}],发送失败。 code:{},msg:{}", TAG, PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_BD_ERROR, response.getMessage());
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PUSH_MSG_SEND_BD_ERROR);
        }
    }

    /**
     * 描述：获取该类的单例对象
     *
     * @return com.zmn.plat.business.impl.sendtemplate.ZmnAliSendSmsInfoBServiceImpl
     * @author wangjie
     * @since 2020/6/19 10:45
     **/
    public static BaiduSendSmsInfoBServiceImpl getSingleton() {
        if (Objects.isNull(aliSendSmsInfoBService)) {
            synchronized (BaiduSendSmsInfoBServiceImpl.class) {
                if (Objects.isNull(aliSendSmsInfoBService)) {
                    aliSendSmsInfoBService = new BaiduSendSmsInfoBServiceImpl();
                }
            }
        }
        return aliSendSmsInfoBService;
    }
}
